package cn.itcast.service.impl;

import cn.itcast.common.DeleteException;
import cn.itcast.dto.DishDto;
import cn.itcast.dto.SetmealDto;
import cn.itcast.entity.Dish;
import cn.itcast.entity.DishFlavor;
import cn.itcast.entity.Setmeal;
import cn.itcast.entity.SetmealDish;
import cn.itcast.mapper.SetmealMapper;
import cn.itcast.service.DishService;
import cn.itcast.service.SetmealDishService;
import cn.itcast.service.SetmealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;


    /**
     * 新增套餐
     * @param setmealDto
     */
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        this.save(setmealDto);

        Long id = setmealDto.getId();
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        List<SetmealDish> collect = setmealDishes.stream().map((item) -> {
            item.setSetmealId(id);
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(collect);
    }


    /**
     * 修改套餐时数据回显
     * @param id
     * @return
     */
    @Override
    public SetmealDto reSee(long id) {
        Setmeal byId = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(byId,setmealDto);

        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,byId.getId());
        List<SetmealDish> list = setmealDishService.list(setmealDishLambdaQueryWrapper);
        setmealDto.setSetmealDishes(list);

        return setmealDto;
    }

    /**
     * 修改套餐
     * @param setmealDto
     */
    @Override
    public void updateWithDish(SetmealDto setmealDto) {

        this.updateById(setmealDto);

        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());

        setmealDishService.remove(setmealDishLambdaQueryWrapper);


        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        List<SetmealDish> collect = setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(collect);

//        List<SetmealDish> collect = setmealDishes.stream().map((item) -> {
//            item.setDishId(setmealDto.getId());
//            return item;
//        }).collect(Collectors.toList());
//        setmealDishService.saveBatch(collect);

    }

    /**
     * 删除套餐 在售的不可以删除,禁售的可以删除
     * @param ids
     */
    @Override
    public void deleteWithDish(List<Long> ids) {

        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.in(ids != null,Setmeal::getId,ids);
        List<Setmeal> list = this.list(setmealLambdaQueryWrapper);
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        for (Setmeal setmeal : list) {
            if (setmeal.getStatus() == 1){
                throw new DeleteException("在售的套餐不能删除");
            }else {
                setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
                setmealDishService.remove(setmealDishLambdaQueryWrapper);
                this.removeById(setmeal.getId());
            }
        }


    }



}
